import Vue from 'vue'
import store from '@/store/index.js'
import Router from 'vue-router'
import BasicLayout from '@/layouts/BasicLayout'
import { ipcRenderer } from 'electron'

import { recommendRoutes } from './modules/recommend'

Vue.use(Router)

const routes = [
  {
    path: '/',
    name: 'index',
    component: BasicLayout,
    redirect: '/home',
    children: [
      ...recommendRoutes
    ]
  }
]

const router = new Router({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.auth) {
    const userId = localStorage.getItem('userId')
    if (userId) {
      next()
    } else {
      // 展示登录框
      // store.commit('User/SET_SHOW_LOGIN', true)
      // store.commit('App/SET_REDIRECT', to.fullPath)
      console.log(store.state.App.redirect)
    }
  } else {
    next()
  }
})

router.afterEach((to, from) => {
  if (to.meta && to.meta.title) {
    // 设置标题
    ipcRenderer.send('set-tray-title', to.meta.title)
  }
})

export default router
